﻿using DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using DBUtility;

namespace DAL
{
    public class ICStockDAL
    {
        /// <summary>
        /// 获取入库单列表
        /// </summary>
        public DataSet GetICStockBill()
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select ROW_NUMBER() OVER (ORDER BY FBrNo)Num,FBrNo,Fname,Fattr,FModel, ");
            commandText.Append(" CONVERT(varchar(100), Fdate, 23)Fdate,b.NAME FBillerID ");
            commandText.Append(" from ICStockBill left join sys_master b on b.ID = FBillerID ");
            commandText.Append(" where FStatus=2 order by FBrNo ");

            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                ds = DbHelperSQL.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        /// <summary>
        /// 获取入库单里的明细
        /// </summary>
        public DataSet GetICStockBillEntry(string FBrNo)
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append("  select PkgsID,PkgsBarCode,a.PlaceBarCode,a.FName PlaceName,Floor, ");
            commandText.Append("  case  SUBSTRING(HelperCode,1,1)  when 'A' then '定尺' when 'B' then '不定尺' end PkgsIsRegular    ");
            commandText.Append("  from stk_Package left join t_StockPlace a on a.FSPID = PosiID ");
            commandText.Append("  where PkgsStatus=99 and InStkBillNo = '" + FBrNo + "' ");
               try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                ds = DbHelperSQL.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        /// <summary>
        /// 获取入库单里的明细
        /// </summary>
        public DataSet GetICStockBillcount(string FBrNo)
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append("select count(countid) as zcount from stk_Package where PkgsStatus=0 and InStkBillNo='" + FBrNo + "'");
            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                ds = DbHelperSQL.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        //根据条码获取产成品入库的条码信息 ql 20181119
        public bool GetICStockBillEntryCode(string PkgsBarCode, string FBrNo, out string FEntryNo, out string PlaceBarCode,out string   PlaceName, out string dc)
        {
            bool result = false;
            DataSet dt = new DataSet();
            FEntryNo = "";
            PlaceBarCode = "";
            dc = ""; PlaceName = "";
            StringBuilder commandText = new StringBuilder();
            //commandText.Append(" select PkgsID FEntryNo,PkgsBarCode,a.PlaceBarCode,a.FNumber PlaceName,Floor,  ");
            //commandText.Append(" case PkgsIsRegular when 1 then '定尺' else '不定尺' end PkgsIsRegular  ");
            //commandText.Append(" from stk_Package left join t_StockPlace a on a.FSPID = PosiID  where PkgsStatus=99 ");
            //commandText.Append(" and InStkBillNo = '" + FBrNo + "' and PkgsBarCode ='" + PkgsBarCode + "'");

            commandText.Append("   select PkgsID FEntryNo,PkgsBarCode,a.PlaceBarCode,a.FNumber PlaceName,Floor,  ");
            commandText.Append("  case  SUBSTRING(HelperCode,1,1)  when 'A' then '定尺' when 'B' then '不定尺' end dc   ");
            commandText.Append("  from stk_Package left join t_StockPlace a on a.FSPID = PosiID  where PkgsStatus=99  ");
            commandText.Append("  and InStkBillNo =  '" + FBrNo + "' and PkgsBarCode ='" + PkgsBarCode + "'");

            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                dt = DbHelperSQL.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    FEntryNo = dr["FEntryNo"].ToString().Trim();
                    PlaceBarCode = dr["PlaceBarCode"].ToString().Trim();
                    dc = dr["dc"].ToString().Trim();
                    PlaceName = dr["PlaceName"].ToString().Trim();
                }
                if (FEntryNo == null || FEntryNo == "") { return false; } else { result = true; }
                if (PlaceBarCode == null || PlaceBarCode == "") { return false; } else { result = true; }
                if (dc == null || dc == "") { return false; } else { result = true; }
                if (PlaceName == null || PlaceName == "") { return false; } else { result = true; }
      
            }
            return result;
        }
        /// <summary>
        /// 获取库位名称
        /// </summary>
        public bool GetPlaceName(string PlaceBarCode, out string FNumber, out string FName)
        {
            bool lb_return = false;
            FNumber = "";
            FName = "";
            DataSet dt = new DataSet();
            StringBuilder sql = new StringBuilder();
            sql.Append(" select FNumber,FName from t_StockPlace where PlaceBarCode = '" + PlaceBarCode + "'");
            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                dt = DbHelperSQL.Query(sql.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    FNumber = dr["FNumber"].ToString().Trim();
                    FName = dr["FName"].ToString().Trim();
                }
            }

            if (FNumber == null || FNumber == "") { lb_return = false; } else { lb_return = true; }
            if (FName == null || FName == "") { lb_return = false; } else { lb_return = true; }

            return lb_return;
        }
        /// <summary>
        /// 更新明细库位信息
        /// </summary>
        public bool UpdateICStockEntry(string PkgsID, string PkgsBarCode, string PlaceBarCode, string dc)
        {
            bool lb_return = false;
            string stockID = ""; string PosiID = "";
            List<string> sql = new List<string>();
            string PkgsIsRegularID = "";
            if (dc == "不定尺")
            {
                PkgsIsRegularID = "2";
            }
            else
            {
                PkgsIsRegularID = "1";
            }

            lb_return = GetStockPosi(PlaceBarCode, out stockID, out PosiID);

            if (lb_return)
            {
                StringBuilder commandText = new StringBuilder();
                commandText.Append(" update stk_Package set ");
                commandText.Append(" PkgsBarCode='" + PkgsBarCode + "'");
                commandText.Append(" ,StockID='" + stockID + "'");
                commandText.Append(" ,PosiID='" + PosiID + "'");
                commandText.Append(" ,dc='" + PkgsIsRegularID + "'");
                commandText.Append(" ,PkgsStatus=0 ");
                commandText.Append(" where PkgsID='" + PkgsID + "'");
                sql.Add("update ICStockBillEntry set PkgsBarCode = '" + PkgsBarCode + "',HelperCode = (select HelperCode from stk_Package where PkgsID='" + PkgsID + "'),StockID='" + stockID + "',PosiID='" + PosiID + "',Floor='23',PkgsIsRegular=(select PkgsIsRegular from stk_Package where PkgsID='" + PkgsID + "') where PkgsID = '" + PkgsID + "'");
                sql.Add(commandText.ToString());
                
                try
                {
                    DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                    int i = DbHelperSQL.ExecuteSqlTran(sql);
                    if (i > 0)
                    { lb_return = true; }
                    else { lb_return = false; }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return lb_return;
        }
        //获取仓库  库位
        public bool GetStockPosi(string PlaceBarCode, out string stockID, out string PosiID)
        {
            bool lb_return = false;
            stockID = ""; PosiID = "";
            DataSet dt = new DataSet();
            StringBuilder sql = new StringBuilder();
            sql.Append(" select FParentID,FSPID from t_StockPlace where PlaceBarCode = '" + PlaceBarCode + "'");
            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                dt = DbHelperSQL.Query(sql.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (dt != null)
            {
                lb_return = true;

                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    stockID = dr[0].ToString().Trim();
                    PosiID = dr[1].ToString().Trim();
                }
            }

            return lb_return;
        }
        //获取层
        public bool GetFloor(string PlaceBarCode, out string Floor)
        {
            bool lb_return = false;
            Floor = "";

            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();

            commandText.Append(" select MAX(a.Floor) Floor");
            commandText.Append(" from stk_Package a , t_StockPlace b ");
            commandText.Append(" where a.PkgsStatus in ('0','1')");
            commandText.Append(" and a.PosiID = b.FSPID ");
            commandText.Append(" and b.PlaceBarCode = '" + PlaceBarCode + "'");

            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                dt = DbHelperSQL.Query(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (dt != null)
            {
                lb_return = true;

                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    Floor = dr[0].ToString().Trim();
                }
            }

            return lb_return;
        }

        /// <summary>
        /// 同步金蝶入库单
        /// </summary>
        /// <param name="FBillNo"></param>
        /// <returns></returns>
        public int INSERT_ICStockBill(string FBillNo)
        {
            //调用金蝶接口方法
            WebReference.jdWebService seo = new WebReference.jdWebService();
            int nums = seo.CCPXT_INSERT_ICStockBill("CCPXT_INSERT_ICStockBill", "" + FBillNo + "");
            return nums;
        }
        //完成入库单
        /// <summary>
        /// 判断是否可以完成入库
        /// </summary>
        /// <returns></returns>
        public bool IsFinishEnter(string fBrNo, out string txt)
        {
            txt = "";
            //先判断审核了没有
            //if (IsEnterExamine(fBrNo)) { } else { txt = "入库不是已审核状态"; return false; }
            //判断明细是否都有库位
            //if (IsEnterDetail(fBrNo)) { } else { txt = "入库明细录入不正确"; return false; }
            //判断入库数量主细表是否正确
            //if (IsEnterNum(fBrNo)) { } else { txt = "入库单数量不正确"; return false; }

            return true;
        }
        //判断入库单状态
        public bool IsEnterExamine(string fBrNo)
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            string ls_status = "";
            commandText.Append(" select FStatus from ICStockBill ");
            commandText.Append(" where FBrNo='" + fBrNo + "'");
            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                ds = DbHelperSQL.Query(commandText.ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ls_status = dr[0].ToString().Trim();
            }

            if (ls_status == "2") { } else { return false; }

            return true;
        }
        //判断入库单明细是否都有库位
        public bool IsEnterDetail(string fBrNo)
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select FEntryNo,StockID,PosiID,FLOOR,PkgsIsRegular from ICStockBillEntry ");
            commandText.Append(" where FBrNo='" + fBrNo + "'");
            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                ds = DbHelperSQL.Query(commandText.ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (ds != null)
            {
                foreach (DataTable dt in ds.Tables)   //遍历所有的datatable
                {

                    foreach (DataRow dr in dt.Rows)
                    {
                        string StockID = dr["StockID"].ToString().Trim(); if (StockID == null || StockID == "" || StockID == "0") { return false; }
                        string PosiID = dr["PosiID"].ToString().Trim(); if (PosiID == null || PosiID == "" || PosiID == "0") { return false; }
                        //string FLOOR = dr["FLOOR"].ToString().Trim(); if (FLOOR == null || FLOOR == "" || FLOOR == "0") { return false; }
                        string PkgsIsRegular = dr["PkgsIsRegular"].ToString().Trim(); if (PkgsIsRegular == null || PkgsIsRegular == "" || PkgsIsRegular == "0") { return false; }
                    }
                }
            }
            else
            {
                return false;
            }


            return true;
        }
        //判断入库数量主细表是否正确
        public bool IsEnterNum(string fBrNo)
        {
            DataSet ds = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select a.FBrNo,a.FAuxQtyFinish,SUM(b.FQty) FQty ");
            commandText.Append(" from ICStockBill a,ICStockBillEntry b");
            commandText.Append(" where a.FBrNo = b.FBrNo ");
            commandText.Append(" and a.FBrNo='" + fBrNo + "' ");
            commandText.Append(" group by a.FBrNo,a.FAuxQtyFinish ");
            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                ds = DbHelperSQL.Query(commandText.ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (ds != null)
            {
                foreach (DataTable dt in ds.Tables)   //遍历所有的datatable
                {

                    foreach (DataRow dr in dt.Rows)
                    {
                        string FAuxQtyFinish = dr["FAuxQtyFinish"].ToString().Trim(); if (FAuxQtyFinish == null || FAuxQtyFinish == "") { return false; }
                        string FQty = dr["FQty"].ToString().Trim(); if (FQty == null || FQty == "") { return false; }

                        if (FAuxQtyFinish == FQty) { } else { return false; }
                    }
                }
            }
            else
            {
                return false;
            }


            return true;
        }
        /// <summary>
        /// 完成入库单
        /// </summary>
        /// <returns></returns>
        public bool FinishEnter(string fBrNo)
        {
            bool lb_return = false;
            DataSet dM = GetEnterFinish(fBrNo);
            DataSet dD = GetEnterFinishDetail(fBrNo);

            List<string> sql = new List<string>();
            //主表
            string FBrNo = DateTime.Now.ToString("yyyyMMddHHmmss").Substring(2, 12);

            if (dM != null)
            {
                foreach (DataTable dt in dM.Tables)   //遍历所有的datatable
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string FItemID = dr["FItemID"].ToString().Trim();
                        string SKU = dr["SKU"].ToString().Trim();
                        string FKFDate = dr["FKFDate"].ToString().Trim();
                        string FBatchNo = dr["FBatchNo"].ToString().Trim();
                        string FQty = dr["FQty"].ToString().Trim();

                        string FQtyLock = dr["FQtyLock"].ToString().Trim();
                        string FUnit = dr["FUnit"].ToString().Trim();
                        string PieceNum = dr["PieceNum"].ToString().Trim();
                        string FModel = dr["FModel"].ToString().Trim();
                        string Fname = dr["Fname"].ToString().Trim();

                        StringBuilder commandText = new StringBuilder();
                        commandText.Append(" insert into ICInventory(FBrNo,FItemID,SKU,FKFDate,FBatchNo,FQty,FQtyLock,FUnit,PieceNum,FModel,Fname)values(");
                        commandText.Append("'" + FBrNo + "',"); commandText.Append("'" + FItemID + "',");
                        commandText.Append("'" + SKU + "',"); commandText.Append("'" + FKFDate + "',");
                        commandText.Append("'" + FBatchNo + "',"); commandText.Append("'" + FQty + "',");
                        commandText.Append("'" + FQtyLock + "',"); commandText.Append("'" + FUnit + "',");
                        commandText.Append("'" + PieceNum + "',"); commandText.Append("'" + FModel + "',");
                        commandText.Append("'" + Fname + "')");

                        sql.Add(commandText.ToString());
                    }
                }
            }

            //细表
            if (dD != null)
            {
                foreach (DataTable dt in dD.Tables)   //遍历所有的datatable
                {
                    int num = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string PkgsBarCode = dt.Rows[i]["PkgsBarCode"].ToString().Trim();
                        string ItemSku = dt.Rows[i]["ItemSku"].ToString().Trim();
                        string StickLen = dt.Rows[i]["StickLen"].ToString().Trim();
                        string Stick = dt.Rows[i]["Stick"].ToString().Trim();
                        string FQty = dt.Rows[i]["FQty"].ToString().Trim();
                        string StockID = dt.Rows[i]["StockID"].ToString().Trim();

                        string PosiID = dt.Rows[i]["PosiID"].ToString().Trim();
                        string Floor = "";//dt.Rows[i]["Floor"].ToString().Trim(); 改为计算层数
                        if (i == 0)
                        {
                            Floor = GetMaxFloor(StockID, PosiID);
                            num = 1;
                        }
                        else
                        {
                            string OStockID = dt.Rows[i - 1]["StockID"].ToString().Trim();
                            if (OStockID == StockID)
                            {
                                Floor = (int.Parse(GetMaxFloor(StockID, PosiID)) + num).ToString();
                                num++;
                            }
                            else
                            {
                                Floor = GetMaxFloor(StockID, PosiID);
                                num = 1;
                            }
                        }

                        string InStkTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        string InStkMan = dt.Rows[i]["InStkMan"].ToString().Trim();
                        string PkgsStatus = dt.Rows[i]["PkgsStatus"].ToString().Trim();
                        string WorkBillNo = dt.Rows[i]["WorkBillNo"].ToString().Trim();

                        string InStkBillNo = fBrNo;
                        string PkgsIsRegular = dt.Rows[i]["PkgsIsRegular"].ToString().Trim();

                        StringBuilder commandText = new StringBuilder();
                        commandText.Append(" insert into stk_Package(FBrNo,PkgsBarCode,ItemSku,StickLen,Stick,FQty,StockID,PosiID,Floor,");
                        commandText.Append(" InStkTime,InStkMan,PkgsStatus,WorkBillNo,InStkBillNo,dc)values(");

                        commandText.Append("'" + FBrNo + "',");
                        commandText.Append("'" + PkgsBarCode + "',"); commandText.Append("'" + ItemSku + "',");
                        commandText.Append("'" + StickLen + "',"); commandText.Append("'" + Stick + "',");
                        commandText.Append("'" + FQty + "',"); commandText.Append("'" + StockID + "',");

                        commandText.Append("'" + PosiID + "',"); commandText.Append("'" + Floor + "',");
                        commandText.Append("'" + InStkTime + "',"); commandText.Append("'" + InStkMan + "',");
                        commandText.Append("'" + PkgsStatus + "',"); commandText.Append("'" + WorkBillNo + "',");

                        commandText.Append("'" + InStkBillNo + "',"); commandText.Append("'" + PkgsIsRegular + "')");

                        sql.Add(commandText.ToString());
                    }
                }
            }

            ///ql1128
            sql.Add("  update stk_Package set PkgsStatus=0 where InStkBillNo='" + fBrNo + "'");
            //

            sql.Add("update ICStockBill set FStatus=3 where FBrNo = '" + fBrNo + "'");


            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                int i = 0;
                i = DbHelperSQL.ExecuteSqlTran(sql);
                if (i > 0)
                { lb_return = true; }
                else
                { lb_return = false; }

            }
            catch (Exception ex)
            {
                throw ex;
            }


            return lb_return;
        }

        public bool FinishEnterN(string fBrNo, string ry,string id)
        {
            bool lb_return = false;
            string fname="";
            List<string> sql = new List<string>();
            string ntime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            StringBuilder commandText = new StringBuilder();
            string sql4 = "select FItemID from t_Emp where FName='" + ry + "'";
            DataTable de = DbHelperSQL.QueryTable(sql4);
            if (de.Rows.Count > 0)
            {
                fname = de.Rows[0]["FItemID"].ToString();
            }
            commandText.Append("  update stk_Package set InStkMan ='" + ry + "',InStkTime ='" + ntime + "' where InStkBillNo='" + fBrNo + "'");

            sql.Add(commandText.ToString());
            sql.Add("update ICStockBill set FStatus=3,FFManagerID='" + fname + "',FSManagerID='" + fname + "' where FBrNo = '" + fBrNo + "'");         
            

            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                int i = 0;
                i = DbHelperSQL.ExecuteSqlTran(sql);
                if (i > 0)
                {
                    lb_return = GetQuantityNum(fBrNo);
                }
                else
                { lb_return = false; }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lb_return;
        }


        public bool GetQuantityNum(string FBrNo)
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            List<string> sql1 = new  List<string>();
            int i = 0;
            bool result = false;
            
            sql.Append( "select SUM(case a.PkgsStatus  when '0' then FQty else 0 end  ) qty,a.WorkBillNo from stk_Package a where WorkBillNo in ( ");
            sql.Append("select distinct WorkBillNo from stk_Package where InStkBillNo ='" + FBrNo + "' ");
            sql.Append(")group by a.WorkBillNo ");

            try
            {
                dt = DbHelperSQL.QueryTable(sql.ToString());
                if (dt != null)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        decimal qty = decimal.Parse(dr["qty"].ToString());
                        string WorkBillNo = dr["WorkBillNo"].ToString(); if (WorkBillNo == null) { result = false; } else { result = true; }

                        sql1.Add("update ICMO set QuantityNum = '" + qty + "' where  FInterID = '" + WorkBillNo + "'");

                    }
                     i = DbHelperSQL.ExecuteSqlTran(sql1);
                }

                if (i > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 获取单条入库单
        /// </summary>
        /// <returns></returns>
        public DataSet GetEnterFinish(string FBrNo)
        {
            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select a.FBrNo FBrNo,b.FItemID FItemID,a.SKU SKU,CONVERT(varchar(100),c.FDate, 20) FKFDate,a.FGMPBatchNo FBatchNo, ");
            commandText.Append(" a.FAuxQtyFinish FQty,0 FQtyLock,a.FUnit FUnit,a.PieceNum PieceNum,a.FModel FModel,a.Fname Fname ");
            commandText.Append(" from ICStockBill a left join t_ICItem b on SUBSTRING(a.SKU,0,CHARINDEX('__',a.SKU)) = b.FNumber ");
            commandText.Append(" left join ICMORpt c on a.FSourceBillNo = c.FInterID ");
            commandText.Append(" where a.FBrNo='" + FBrNo + "'");
            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                dt = DbHelperSQL.Query(commandText.ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 获取单条入库单
        /// </summary>
        /// <returns></returns>
        public DataSet GetEnterFinishDetail(string FBrNo)
        {
            DataSet dt = new DataSet();
            StringBuilder commandText = new StringBuilder();
            commandText.Append(" select a.PkgsBarCode PkgsBarCode,a.ItemSku ItemSku,a.StickLen StickLen,a.Stick Stick, ");
            commandText.Append(" a.FQty FQty,a.StockID StockID,a.PosiID PosiID,a.Floor Floor,0 PkgsStatus,b.FBillerID InStkMan, ");
            commandText.Append(" SUBSTRING(b.FSourceBillNo,0,LEN(b.FSourceBillNo)-1) WorkBillNo,a.PkgsIsRegular PkgsIsRegular ");
            commandText.Append(" from ICStockBillEntry a left join ICStockBill b on a.FBrNo = b.FBrNo ");
            commandText.Append(" where a.FBrNo='" + FBrNo + "'");
            commandText.Append(" order by a.StockID,a.PosiID ");

            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                dt = DbHelperSQL.Query(commandText.ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 计算层
        /// </summary>
        /// <returns></returns>
        public string GetMaxFloor(string StockID, string PosiID)
        {
            string ls_return = "";
            string floor = "";
            DataSet dt = new DataSet();

            StringBuilder commandText = new StringBuilder();

            commandText.Append(" select MAX(FLOOR) FLOOR ");
            commandText.Append(" from stk_Package ");
            commandText.Append(" where PkgsStatus in (0,1,88) ");
            commandText.Append(" and StockID = " + StockID);
            commandText.Append(" and PosiID = " + PosiID);

            try
            {
                DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                dt = DbHelperSQL.Query(commandText.ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (dt != null)
            {
                foreach (DataRow dr in dt.Tables[0].Rows)
                {
                    floor = dr[0].ToString().Trim();
                }
            }
            if (floor == "" || floor == null)
            {
                ls_return = "1";
            }
            else
            {
                int maxfloor = 0;
                maxfloor = int.Parse(floor) + 1;
                ls_return = maxfloor.ToString();
            }

            return ls_return;
        }

    }
}
